<template>
  <div>
    <div class="pageheader">
      <XHeader :left-options="{backText:'', preventGoBack: true}" :title="xheader.title" class="XHeader"
               @on-click-back="goBack"></XHeader>
    </div>

    <div class="pagetop">
      <div class="topbar block">
        <form class="inputbox flex flex-center">
          <chimage class="icon" mode="aspectFit" :src="require('@/assets/img/ic_search.png')"></chimage>
          <input
            type="search"
            focus="true"
            style="text-align: left"
            v-model="keyword"
            class="input flex-full"
            placeholder="搜索商品"
            @keyup.enter="onSearch()"
          >
          <div class="searchSubmit" @click="onSearch()">搜索</div>
        </form>
      </div>
    </div>

    <!--筛选排序-->
    <screen-item @onSearch="onSearch" :categoryList="categoryList"/>

    <div>
      <div style="margin-top: 12px" class="swiper-block" v-if="bannerList.length">
        <swiper class="block swiper-container-banner" :options="swiperOptionBanner" style="padding: 0 12px">
          <swiper-slide v-for="(item) in bannerList" :key="item.id">
            <img :src="item.banner_image">
          </swiper-slide>
        </swiper>
        <div class="swiper-pagination" id="swiper-pagination-banner"></div>
      </div>

      <scroller
        @on-pullup-loading="getTopicGoods(searchParams)"
        lock-x
        ref="scroller"
        :pullup-config="{content:'',upContent:'',downContent:'释放加载更多',loadingContent:'加载中...',}"
        height="-45"
        :use-pullup="true"
        style=" padding-bottom:72px;"
        v-model="status"
      >
        <div class="block page" style="margin-top: 12px;padding-bottom: 12px">
          <!--商品列表-->
          <div class="grid grid-gutter specialbox">
            <div v-for="(goods,index) in goodsList" :key="index" class="grid-1-2">
              <div class="item" :class="[isHasGoodsText(goods) ? 'noStock' : '']" @click="onClickGoods(goods)">
                <div class="noStock-back" v-if="isHasGoodsText(goods)"></div>
                <div class="noStock-con">
                  <div class="supplier">
                    <img :src="goods.goods_image">
                    <div class="supplier-text">{{textSupplier(goods.supplier_id)}}</div>
                  </div>
                  <div class="noStock-tip" v-if="isHasGoodsText(goods)">
                    <div class="noStock-tip-text">无货</div>
                  </div>
                </div>
                <div class="title">{{goods.goods_name}}</div>
                <div class="list-price">
                  <div class="price newPrice">{{goods.price_alias}}￥{{goods.goods_price}}</div>
                  <div v-if="!isHasGoodsText(goods)" class="shop-btn"
                       @click.stop="onAddCart(goods.goods_id, goods.goods_sku, goods.goods_mininum)">
                    <img :src="require('@/assets/img/icon_item_gwc.png')"/>
                  </div>
                </div>

                <div class="list-price">
                  <div class="price oldPrice">{{goods.price_alias_market}}￥{{goods.goods_price_market}}</div>
                </div>

                <div class="list-price">
                  <div v-if="discountClass(goods.goods_price_discount)" class="discount"
                       :class="discountClass(goods.goods_price_discount)">
                    <chimage class="icon" :src="require('@/assets/img/icon_discount.png')"></chimage>
                    <span>{{goods.goods_price_discount}}%</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>

        <load-more
          v-if="!more&&!goodsList.length"
          :show-loading="false"
          :tip="'暂无数据'"
          background-color="#fbf9fe"
        ></load-more>
      </scroller>
    </div>
  </div>
</template>

<script>
  import Mall from '../../../dll/mall'
  // import Swiper from 'swiper'
  // import 'swiper/dist/css/swiper.css'
  import { swiper, swiperSlide } from 'vue-awesome-swiper'
  import 'swiper/dist/css/swiper.css'
  import shop from '@/mixin/shop'
  import ScreenItem from '../../../components/screen'

  export default {
    name: 'special',
    mixins: [shop],
    components: {
      swiper,
      swiperSlide,
      ScreenItem
    },
    data: () => {
      return {
        xheader: {
          title: '专题活动'
        },
        // 搜索关键词
        keyword: '',
        // 专题ID
        topicId: 0,
        // 专题banner
        bannerList: [],
        swiperOptionBanner: {
          spaceBetween: 12,
          autoplay: 2000,
          centeredSlides: true,
          disableOnInteraction: false,
          pagination: {
            el: '.swiper-pagination'
          }
        },
        // 专题商品
        goodsList: [],
        // 筛选
        searchParams: {
          page: 1,
          page_size: 20
        },
        // 分类列表
        categoryList: [],
        more: true,
        so: {
          limit: 20,
          page: 1
        },
        status: {
          pullupStatus: 'default'
        }
      }
    },
    created () {
      this.topicId = this.$route.params.topicId
      this.searchParams.topic_id = this.topicId
      this.getTopicBanner()
      this.getTopicGoods()
    },
    watch: {
    },
    methods: {
      // 获取专题banner列表
      async getTopicBanner () {
        this.$vux.loading.show({ text: '数据加载中' })
        let param = { topic_id: this.topicId }
        let res = await Mall.getTopicBanner(param)
        this.$vux.loading.hide()
        if (res.code == 0) {
          this.bannerList = res.data
        } else {
          this.$vux.confirm.show({
            title: '提示',
            content: res.msg,
            showCancelButton: false,
            onConfirm: () => {
              this.$router.back()
            }
          })
        }
      },

      // 获取专题商品列表
      async getTopicGoods (searchParams = {}) {
        // 搜索关键词
        this.searchParams.keyword = this.keyword
        // 地址信息
        let addressAreas = {
          areaCode: '',
          areaName: '',
          areaList: []
        }
        if (localStorage.localSelectArea) {
          addressAreas = JSON.parse(localStorage.localSelectArea)
        }
        let query_params = Object.assign({}, this.searchParams)
        query_params.area_code = addressAreas.areaCode
        this.$vux.loading.show({ text: '数据加载中...' })
        let res = await Mall.getTopicGoods(query_params)
        this.$vux.loading.hide()
        if (res.code == 0) {
          this.isScreen = true
          this.cateGoodsCount = res.data.cate_goods_count
          this.supplierList = res.data.supplier_list
          if (this.searchParams.page >= res.data.page_info.page_total) {
            this.more = false
            this.$refs.scroller.disablePullup()
          } else {
            this.$refs.scroller.donePullup()
          }

          this.goodsList = [].concat(this.goodsList, res.data.list)
          this.searchParams.page = this.searchParams.page + 1

          if (res.data.list.length) {
            this.getCategoryAggs()
          }
        } else {
          this.more = false
          this.$refs.scroller.disablePullup()
          this.$vux.confirm.show({
            title: '提示',
            content: res.msg,
            showCancelButton: false,
            onConfirm: () => {
              this.$router.back()
            }
          })
        }
      },

      // 获取分类商品聚合
      async getCategoryAggs () {
        let params = {
          keyword: this.keyword,
          topic_id: this.topicId
        }
        let res_category = await Mall.getCategoryAggs(params)
        if (res_category.code == 0) {
          this.categoryList = res_category.data.count_category_goods
        } else {
          this.showCateLabel = false
          this.$vux.alert.show({
            title: '提示',
            content: res_category.msg
          })
        }
      },
      // 跳转商品详情
      onClickGoods (goods) {
        this.$router.push({ name: 'mallInfo', params: { goodsId: goods.goods_id } })
      },

      onSearch (searchParams = {}) {
        this.searchParams.page = 1
        this.searchParams.category_id = searchParams.category_id
        this.searchParams.price = searchParams.price
        this.searchParams.sort_type = searchParams.sort_type
        this.searchParams.sort_desc = searchParams.sort_desc
        this.more = true
        this.goodsList = []
        this.status.pullupStatus = 'disabled'
        this.$nextTick(function () {
          this.$refs.scroller.enablePullup()
          this.$refs.scroller.reset({
            top: 0
          })
          this.status.pullupStatus = 'default'
        })
        this.getTopicGoods(searchParams)
      },

      

      // 返回首页
      goBack () {
        // this.$router.push({ name: 'mall' })
        if (this.isCQDX) {
          this.$router.push({ name: 'mall' })
        } else {
          this.$router.go(-1)
        }
      }
    }
  }
</script>

<style lang="less" scoped>
  .swiper {
    height: 206px;
    background-color: #fff;
  }

  .slide-image {
    height: 206px;
    width: 100%;
  }

  .specialbox {
    .item {
      display: block;
      background-color: #fff;
      padding: 12px;
      box-sizing: border-box;
    }

    .item {
      .noStock-con {
        img {
          display: block;
          height: auto;
          width: 100%;
          background-color: #fbf8f7;
          position: absolute;
          left: 50%;
          top: 50%;
          transform: translate(-50%, -50%);
        }

        .supplier {
          height: 145px;
          overflow: hidden;
        }
      }

      > .title {
        margin-top: 16px;
        font-size: 12px;
        color: #575757;
        line-height: 17px;
        height: 34px;
        text-overflow: ellipsis;
        overflow: hidden;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
      }

      > .pricebar {
        display: flex;
        align-items: center;
      }

      .price {
        flex: 1;
        font-size: 14px;
        color: #ae2309;
        line-height: 16px;
      }

      .d-price {
        font-size: 10px;
        color: #d1c8c6;
        line-height: 11px;
        text-decoration: line-through;
      }
    }
  }

  .page {
    padding-top: 84px;
  }

  /deep/ .pageheader {
    z-index: 2 !important;
  }

  .pagetop {
    display: block;
    width: 100%;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 2;
  }

  .topbar {
    box-sizing: border-box;
    background-color: #fff;
    padding-top: 5px;
    padding-bottom: 5px;
  }

  .topbar .icon {
    width: 20px;
    height: 20px;
    padding: 0 5px;
    display: block;
  }

  .topbar .inputbox {
    background: #fbf8f7;
    height: 35px;
  }

  .searchSubmit {
    height: 35px;
    line-height: 35px;
    font-size: 12px;
    border: none;
    background-color: transparent;
    color: #351009;
    width: 60px;
    text-align: center;
  }

  .topbar .input {
    font-size: 12px;
    color: #351009;
    text-align: center;
    line-height: 17px;
  }

 /deep/ .videoPlayback .videoPlayback-con {
   object-fit: contain !important;
 }

</style>
